package com.fcscs.cservice.domain;

import java.util.Comparator;
import java.util.List;

import com.fcscs.core.common.MainEnums.Language;
import com.fcscs.core.domain.HistoricalEntity;
import com.fcscs.cservice.common.CServiceCommonEnums.TemplateType;

public class NotifTemplate extends HistoricalEntity {

	private static final long serialVersionUID = -6036819522118384297L;
	
	private TemplateType tmplType;
	private Language language = Language.ENGLISH;
	private String title;
	private String detail;

	public void setTmplTypeEnum(TemplateType tmplType) {
		this.tmplType = tmplType;
	}

	public void setTmplType(String tmplType) {
		this.tmplType = tmplType == null ? null : TemplateType.valueOf(tmplType);
	}

	public TemplateType getTmplTypeEnum() {
		return tmplType;
	}

	public String getTmplType() {
		return tmplType == null ? null : tmplType.toString();
	}

	public void setLanguageEnum(Language language) {
		this.language = language;
	}

	public void setLanguage(String language) {
		this.language = language == null ? null : Language.valueOf(language);
	}

	public Language getLanguageEnum() {
		return language;
	}

	public String getLanguage() {
		return language == null ? null : language.toString();
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public String getDetail() {
		return detail;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	@Override
	public DBLoc getDBLoc() {
		return DBLoc.GUEST;
	}

	public static class NotifTemplateComparator implements Comparator<NotifTemplate> {

		@Override
		public int compare(NotifTemplate o1, NotifTemplate o2) {
			int result = o1.getTmplTypeEnum().compareTo(o2.getTmplTypeEnum());
			return result;
		}
	}

	private static Comparator<NotifTemplate> comparator = new NotifTemplateComparator();

	public static Comparator<NotifTemplate> getComparator() {
		return comparator;
	}

	/**
	 * the list should be sorted with FieldEditabilityComparator.comparator
	 * @param list
	 * @param category
	 * @param field
	 * @return
	 */
    public static int binarySearch(List<NotifTemplate> list, TemplateType type) {
    	int low = 0;
    	int high = list.size()-1;

    	while (low <= high) {
    	    int mid = (low + high) >>> 1;
    	    NotifTemplate midVal = list.get(mid);
    	    int cmp = midVal.getTmplTypeEnum().compareTo(type);

    	    if (cmp < 0)
    		low = mid + 1;
    	    else if (cmp > 0)
    		high = mid - 1;
    	    else
    		return mid; // key found
    	}
    	return -(low + 1);  // key not found
    }

    @Override
    public String toString() {
    	return tmplType.toString() + "-" + language + ":" + title + ", " + detail;
    }
}
